package com.example.service.impl;

import com.example.entity.CourseSelectionResult;
import com.example.entity.example.CourseSelectionResultExample;
import com.example.mapper.CourseSelectionResultMapper;
import com.example.service.CourseSelectionResultService;
import com.example.util.CustomConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class CourseSelectionResultServiceImpl implements CourseSelectionResultService {
    @Autowired(required = false)
    CourseSelectionResultMapper courseSelectionResultMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add(CourseSelectionResult courseSelectionResult) {
        String studentId = courseSelectionResult.getStudentId();
        String courseId = courseSelectionResult.getCourseId();
        if (this.findByStudentIdAndCourseId(studentId, courseId) != null) {
            throw new RuntimeException(CustomConstant.MESSAGE_THE_ENTRY_ALREADY_EXISTS);
        }
        courseSelectionResultMapper.insert(courseSelectionResult);
    }

    @Override
    public List<CourseSelectionResult> findByStudentId(String studentId) {
        CourseSelectionResultExample example = new CourseSelectionResultExample();
        CourseSelectionResultExample.Criteria criteria = example.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        return courseSelectionResultMapper.selectByExample(example);
    }

    @Override
    public List<CourseSelectionResult> findByCourseId(String courseId) {
        CourseSelectionResultExample example = new CourseSelectionResultExample();
        CourseSelectionResultExample.Criteria criteria = example.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        return courseSelectionResultMapper.selectByExample(example);
    }

    @Override
    public CourseSelectionResult findByStudentIdAndCourseId(String studentId, String courseId) {
        CourseSelectionResultExample example = new CourseSelectionResultExample();
        CourseSelectionResultExample.Criteria criteria = example.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        criteria.andCourseIdEqualTo(courseId);
        List<CourseSelectionResult> courseSelectionResults = courseSelectionResultMapper.selectByExample(example);
        if (courseSelectionResults == null || courseSelectionResults.isEmpty()) {
            return null;
        }
        return courseSelectionResults.get(0);
    }

    @Override
    @Transactional
    public void updateByStudentIdAndCourseId(CourseSelectionResult courseSelectionResult) {
        String studentId = courseSelectionResult.getStudentId();
        String courseId = courseSelectionResult.getCourseId();
        CourseSelectionResultExample example = new CourseSelectionResultExample();
        CourseSelectionResultExample.Criteria criteria = example.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        criteria.andCourseIdEqualTo(courseId);
        courseSelectionResultMapper.updateByExample(courseSelectionResult, example);
    }

    @Override
    @Transactional
    public void updateSelectiveByStudentIdAndCourseId(CourseSelectionResult courseSelectionResult) {
        String studentId = courseSelectionResult.getStudentId();
        String courseId = courseSelectionResult.getCourseId();
        CourseSelectionResultExample example = new CourseSelectionResultExample();
        CourseSelectionResultExample.Criteria criteria = example.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        criteria.andCourseIdEqualTo(courseId);
        courseSelectionResultMapper.updateByExampleSelective(courseSelectionResult, example);
    }

    @Override
    @Transactional
    public void removeByStudentIdAndCourseId(String studentId, String courseId) {
        CourseSelectionResultExample example = new CourseSelectionResultExample();
        CourseSelectionResultExample.Criteria criteria = example.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        criteria.andCourseIdEqualTo(courseId);
        courseSelectionResultMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public void updateSelectiveById(CourseSelectionResult courseSelectionResult) {
        courseSelectionResultMapper.updateByPrimaryKeySelective(courseSelectionResult);
    }


    @Override
    @Transactional
    public void removeById(Integer id) {
        courseSelectionResultMapper.deleteByPrimaryKey(id);
    }
}
